package org.example.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserPublishLike {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/your_database_name";
    private static final String DB_USER = "your_database_username";
    private static final String DB_PASSWORD = "your_database_password";

    public static void main(String[] args) {
        UserPublishLike userPublishLike = new UserPublishLike();
        userPublishLike.likePublish(1, 1);
        userPublishLike.cancelLike(1, 1);
    }

    public void likePublish(int userId, int publishId) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
            String checkSql = "SELECT * FROM user WHERE id = ? AND publish_like = ?";
            preparedStatement = connection.prepareStatement(checkSql);
            preparedStatement.setInt(1, userId);
            preparedStatement.setInt(2, publishId);
            resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                // 用户已经点赞过，取消点赞
                String deleteSql = "DELETE FROM user WHERE id = ? AND publish_like = ?";
                preparedStatement = connection.prepareStatement(deleteSql);
                preparedStatement.setInt(1, userId);
                preparedStatement.setInt(2, publishId);
                preparedStatement.executeUpdate();
                System.out.println("用户" + userId + "取消了对帖子" + publishId + "的点赞");
            } else {
                // 用户没有点赞过，实现点赞功能
                String insertSql = "INSERT INTO user (id, publish_like) VALUES (?, ?)";
                preparedStatement = connection.prepareStatement(insertSql);
                preparedStatement.setInt(1, userId);
                preparedStatement.setInt(2, publishId);
                preparedStatement.executeUpdate();
                System.out.println("用户" + userId + "点赞了帖子" + publishId);
            }

            // 同步更新publish表中的like列
            String updateSql = "UPDATE publish SET like = like + 1 WHERE id = ?";
            preparedStatement = connection.prepareStatement(updateSql);
            preparedStatement.setInt(1, publishId);
            preparedStatement.executeUpdate();
            System.out.println("帖子" + publishId + "的点赞总量加1");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void cancelLike(int userId, int publishId) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
            String checkSql = "SELECT * FROM user WHERE id = ? AND publish_like = ?";
            preparedStatement = connection.prepareStatement(checkSql);
            preparedStatement.setInt(1, userId);
            preparedStatement.setInt(2, publishId);
            resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                // 用户已经点赞过，取消点赞
                String deleteSql = "DELETE FROM user WHERE id = ? AND publish_like = ?";
                preparedStatement = connection.prepareStatement(deleteSql);
                preparedStatement.setInt(1, userId);
                preparedStatement.setInt(2, publishId);
                preparedStatement.executeUpdate();
                System.out.println("用户" + userId + "取消了对帖子" + publishId + "的点赞");
            } else {
                System.out.println("用户" + userId + "没有点赞过帖子" + publishId);
            }

            // 同步更新publish表中的like列
            String updateSql = "UPDATE publish SET like = like - 1 WHERE id = ?";
            preparedStatement = connection.prepareStatement(updateSql);
            preparedStatement.setInt(1, publishId);
            preparedStatement.executeUpdate();
            System.out.println("帖子" + publishId + "的点赞总量减1");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
